<template>
  <InlineCustomColumn
    ref="inputWidget"
    v-bind="$attrs"
    :disabled="disabled"
    :size="size"
    :clearable="clearable"
    :collapseTags="collapseTags"
    :placeholder="placeholder"
    :widget="widget"
    :multiple="multiple"
    :editComponent="OnlineDictSelect"
    @change="onChange"
  >
    <template v-slot="scope">
      <span>{{ getShowValue(scope.row) }}</span>
    </template>
  </InlineCustomColumn>
</template>

<script>
import InlineCustomColumn from '@/components/TableBox/InlineCustomColumn.vue';
import OnlineDictSelect from './index.vue';

export default {
  name: 'InlineCustomDataSelect',
  props: {
    disabled: {
      type: Boolean,
      default: false,
    },
    size: {
      type: String,
    },
    clearable: {
      type: Boolean,
      default: false,
    },
    collapseTags: {
      type: Boolean,
      default: true,
    },
    placeholder: {
      type: String,
    },
    widget: {
      type: Object,
    },
    multiple: {
      type: Boolean,
      default: false,
    },
  },
  components: {
    InlineCustomColumn,
  },
  inject: ['form'],
  data() {
    return {
      OnlineDictSelect: OnlineDictSelect,
    };
  },
  methods: {
    onChange(value, row, selectRow, dictData) {
      this.$emit('input', value);
      this.$emit('change', value, row, selectRow, dictData);
    },
    getShowValue(row) {
      if (row == null) return '';
      if (row.isAggregate && row.groupField != null) {
        if (this.$attrs['row-group-node'] !== true) return '';
        let childrenList = row._X_ROW_CHILD_LIST;
        if (Array.isArray(childrenList) && childrenList.length > 0) {
          let data = childrenList[0];
          let temp = '';
          if (data[row.groupField + 'DictMap'] != null) {
            temp = data[row.groupField + 'DictMap'].name;
          } else if (data[row.groupField + 'DictMapList'] != null) {
            temp = data[row.groupField + 'DictMapList'].map(item => item.name).join(',');
          } else {
            temp = data[row.groupField];
          }
          return `${temp}【${childrenList.length}】`;
        }
      }
      let field = this.$attrs.field;
      if (row[field + 'DictMap']) return row[field + 'DictMap'].name;
      if (row[field + 'DictMapList'] && Array.isArray(row[field + 'DictMapList'])) {
        return row[field + 'DictMapList'].map(item => item.name).join(',');
      }
      return row[field];
    },
    focus() {
      if (this.$refs.inputWidget && this.$refs.inputWidget.focus) this.$refs.inputWidget.focus();
    },
  },
};
</script>

<style></style>
